// ==UserScript==
// @name         Evolve Script
// @namespace    http://tampermonkey.net/
// @version      0.0.1
// @description  try to take over the world!
// @author       You
// @match        https://g8hh.github.io/evolve/
// @match        https://pmotschmann.github.io/Evolve/
// @icon         https://www.google.com/s2/favicons?sz=64&domain=github.io
// @grant        none
// ==/UserScript==

(function () {
    'use strict';

    setInterval(function () {
        Rna_click("#evolution-rna a");
    });

    setInterval(function () {
        Rna_click("#evolution-dna a");
    });

    setInterval(function () {
        Rna_click("#city-lumber a");
    });

    setInterval(function () {
        Rna_click("#city-food a");
    });

    setInterval(function () {
        Rna_click("#city-stone a");
    });

    add_button();
    add_money();

})();

function add_button() {
    let resources = document.querySelector("#resources");
    if (resources) {
        let button=document.createElement("button");
        button.innerHTML = "增加资源";
        button.addEventListener("click",add_resource);
        resources.appendChild(button);
    }
}

function add_money() {
    let resources = document.querySelector("#resources");
    if (resources) {
        let button=document.createElement("button");
        button.innerHTML = "增加钱知识";
        button.addEventListener("click",add_money_max);
        resources.appendChild(button);
    }
}

function add_money_max() {
    let global_data = window.localStorage.getItem('evolved') || false;
    if (global_data) {
        let saveState = JSON.parse(LZString.decompressFromUTF16(global_data));
        saveState.resource["Money"].amount = saveState.resource["Money"].max;
        saveState.resource["Knowledge"].amount = saveState.resource["Knowledge"].max;
        window.localStorage.setItem('evolved', LZString.compressToUTF16(JSON.stringify(saveState)));
        window.location.reload();
    }
}


function add_resource() {
    let global_data = window.localStorage.getItem('evolved') || false;
    if (global_data) {
        let saveState = JSON.parse(LZString.decompressFromUTF16(global_data));
        // console.log('处理前：',saveState.resource);
        for (let key in saveState.resource) {
            if (saveState.resource[key].display) {
                if (saveState.resource[key].max===-1) {
                    saveState.resource[key].amount += 20000;
                }else{
                    saveState.resource[key].amount = saveState.resource[key].max;
                }
            }
        }
        // console.log('处理后：',saveState.resource);
        window.localStorage.setItem('evolved', LZString.compressToUTF16(JSON.stringify(saveState)));
        window.location.reload();
    }
}

function Rna_click(slector) {
    var rna = document.querySelector(slector);
    if (rna) {
        rna.click();
    }
}